package SMTPSender;

use Net::SMTP;
use MIME::Lite;

# use lib $ENV{SB_LIB};
use Utils; 


sub new 
{
    my $class   	 = shift;
    my $smtp_host_ip = shift;

    my $utils	= new Utils();
    my $sb_dates	= new SBDates();
    # 	my $backup_addr	= $sb_dates->{thisyear} . '-' . $sb_dates->{thismonth} . '@mailbackup.confero24.com';
    my $backup_addr	= 'news@indescon.org';

    my $self 	= 
    {
            smtp_host_ip 		=> $smtp_host_ip  || '193.197.159.253',
            default_sender_profile 	=> 1,
            backup_addr		=> $backup_addr,
            DefaultSenderAddress	=> $utils->getProperty('SenderEmailAddress'),
    };

    bless $self, 'SMTPSender';
    return $self;
}

sub sendMIMEEmail
{
    my ($self, %email)	= @_;

    my $message_body= $email{message} . "\n\n";

    my $msg = 	MIME::Lite->new 
                (
                    From    =>  "$email{sender_name} <$email{sender_email}>",
                    To      =>  $email{recipient_email},
                    Subject =>  $email{subject},
                    CC      =>  $email{cc},
                    Bcc     =>  $self->{'backup_addr'},
                    Type    =>  'multipart/mixed'
                ) or die "Error creating multipart container: $!\n";

    $msg->attach (
            Type 	=> 'TEXT/HTML',
            Data 	=> $message_body
    ) or die "Error adding the text message part: $!\n";

    $msg->attach (
            Type 		=> $email{attachment_type},
            Path 		=> $email{attachment_path},
            Filename 	=> $email{attachment_name},
            Disposition 	=> 'attachment'
    ) or die "Error adding $attachment_type from $attachment_path: $!\n";

    my $utils	= new Utils();

    $msg->send('smtp', $Utils::config{'SMTPServer'}, Timeout=>60) || die "\nSending with attachment failed\n";
    return;
}

sub sendMIMEEmail_Text
{
    my ($self, %email)	= @_;

    my $message_body= $email{message} . "\n\n";
    my $utils	= new Utils();

    my $msg = 	MIME::Lite->new 
                (
                    From    =>  "$email{sender_name} <$email{sender_email}>",
                    To      => 	$email{recipient},
                    Subject => 	$email{subject} . ' - [' . $tag_id . ']',
                    CC	    =>	$email{cc},
                    Bcc     => 	$self->{'backup_addr'},
                    Type    => 	'TEXT',
                    Data    => 	$message_body,
                ) or die "Error creating multipart container: $!\n";

    MIME::Lite->send('smtp', $utils->getProperty('SMTPServer'), Timeout=>60) || die "Failed SMTP conn for simple email. $!\n";
    $msg->send || die "Sending simple text mail failed. $!\n";
    return $tag_id;
}

sub sendMIMEEmail_HTML
{
    my ($self, %email)	= @_;

    my $utils       = new Utils();
    my $message_body= $email{message} . "\n\n";

    my $msg     = MIME::Lite->new 
                (
                    From    =>  "$email{sender_name} <$email{sender_email}>",
                    To      =>  $email{recipient_email},
                    Subject =>  $email{subject} . ' - [' . $tag_id . ']',
                    CC      =>  $email{cc},
                    Bcc     =>  $self->{'backup_addr'},
                    Type    =>  'multipart/related',
                ) or die "Error creating multipart container: $!\n";

    $msg->attach (
            Type    => 'text/html',
            Data    => $message_body,
            Filename=> $email{filename},
    ) or die "Error adding the HTML message part: $!\n";

    MIME::Lite->send('smtp', $Utils::config{'SMTPServer'}, Timeout=>60) || die "Failed SMTP conn for simple email. $!\n";
    $msg->send || die "Sending simple text mail failed. $!\n";
    return $tag_id;
}

sub getFooter
{
	my $utils	= new Utils;
	return "\n\n_______________________________________________________________\nhttp://sellerbay.confero24.com | Tools for ebay(TM) Selling \nYour order has been processed by SellerBay " . $utils->getProperty('VERSION');
}

1;